import {
    ipcMain,
    app
  } from "electron";
  
  export default class BaseWindow {
    constructor() {
      this.win = null;
      this.isLoaded = false;
      this.url = null;
      this.name = null;
      this.positionX = null; // 最大化时候的坐标
      this.positionY = null; // 最大化时候的坐标
      this.listeners = [];
  
      this.registerListener("minimize-window", (event, name) => {
        if (this.name === name) {
          this.win.minimize();
        }
      });
  
      this.registerListener("maximize-window", (event, name, type) => {
        if (this.name === name) {
          if (type === "maximize") {
            const winPositions = this.win.getPosition();
            this.positionX = winPositions[0];
            this.positionY = winPositions[1];
            this.win.maximize();
          }
          if (type === "reverseMaximize") {
            // this.win.setContentSize(860, 660); // 窗口大小
            this.win.setContentSize(920, 680); // 窗口大小
            this.win.setPosition(this.positionX, this.positionY); // 窗口位置坐标
            this.isMax = true;
  
            // 全屏状态下恢复
            if (this.win.isFullScreen()) {
              this.win.setFullScreen(false);
            }
          }
        }
      });
  
      this.registerListener("close-window", (event, name) => {
        if (this.name === name) {
          this.close();
        }
      });
  
      this.registerListener("hide-window", (event, name) => {
        if (this.name === name) {
          this.hide();
        }
      });
  
      this.registerListener("resize-window", (event, name, width, height) => {
        if (this.name === name) {
          this.win.setContentSize(width, height);
        }
      });
  
      // 重置弹窗位置
      this.registerListener("reset-position", (event, name, x, y) => {
        if (this.name === name) {
          const winPositions = app.mainWindow.win.getPosition();
          this.win.setPosition(winPositions[0], winPositions[1]);
        }
      });
      // 重置鼠标点击位置
      this.registerListener("move-to-mouse-position", (event, name, x, y) => {
        if (this.name === name) {
          const height = this.win.getSize()[1]; // 获取窗口的高度
          const weight = this.win.getSize()[0]; // 获取窗口的宽度
          y = y - height / 2; // 计算窗口显示的高度坐标
          y = y < 0 ? 0 : y;
          x = x - weight - 5; // 计算窗口显示的宽度坐标
          x = x < 0 ? 0 : x;
          this.win.setPosition(parseInt(x), parseInt(y));
        }
      });
    }
  
    registerListener(name, fun) {
      this.listeners.push({ name: name, fun: fun });
      ipcMain.on(name, fun);
    }
  
    loadURL() {
      this.win.loadURL(this.url);
      this.isLoaded = true;
    }
  
    show() {
      if (this.win === null || this.url === null) {
        return;
      }
  
      if (!this.isLoaded) {
        this.loadURL();
        this.win.show();
        return;
      }
  
      this.win.show();
      this.win.setSkipTaskbar(false);
    }
  
    hide() {
      if (this.win === null) {
        return false;
      }
  
      this.win.hide();
      return true;
    }
  
    close() {
      if (this.win === null) {
        return false;
      }
  
      this.unregisterAllListeners();
  
      this.isLoaded = false;
      if (this.win && !this.win.isDestroyed()) {
        this.win.close();
      }
      return true;
    }
  
    unregisterAllListeners() {
      if (this.listeners === null) {
        return;
      }
  
      this.listeners.forEach(lis => {
        ipcMain.removeListener(lis.name, lis.fun);
      });
      this.listeners = null;
    }
  
    openDevTools() {
      // if (process.env.NODE_ENV === "development" || global.devToolsEnable) {
      //   this.win.webContents.openDevTools();
      // }
      
      this.win.webContents.openDevTools();
    }
  
    closeDevTools() {
      this.win.webContents.closeDevTools();
    }
  
    isDevToolsOpened() {
      return this.win.webContents.isDevToolsOpened();
    }
  }
  